<?php
namespace api\modules\ua\models;

class Request
{
    // Curl POST 方法
    public function curlPost($url,$data=[])
    {
        $ch = curl_init();
        $params[CURLOPT_URL] = $url;    //请求url地址
        $params[CURLOPT_HEADER] = false; //是否返回响应头信息
//        $params[CURLOPT_HTTPHEADER] = ['Content-Type: application/json', 'Accept: application/json'];
        $params[CURLOPT_SSL_VERIFYPEER] = false;
        $params[CURLOPT_SSL_VERIFYHOST] = false;
        $params[CURLOPT_TIMEOUT] = 30; //是否将结果返回
        $params[CURLOPT_RETURNTRANSFER] = true; //是否将结果返回
        $params[CURLOPT_POST] = true;
        $params[CURLOPT_POSTFIELDS] = $data;

        curl_setopt_array($ch, $params); //传入curl参数
        $content = curl_exec($ch); //执行
        curl_close($ch); //关闭连接
        return $content;
    }

    // Curl Get 方法
    public function curlGet($url, $params = [])
    {
        // 如果有参数，则将其附加到 URL
        if (!empty($params)) {
            $url .= '?' . http_build_query($params);
        }

        $ch = curl_init();
        curl_setopt($ch, CURLOPT_URL, $url);
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
        curl_setopt($ch, CURLOPT_HEADER, 0);
        $output = curl_exec($ch);

        curl_close($ch); //关闭连接
        return $output;
    }

//    public function qmGet($url, $headers)
//    {
//        $ch = curl_init();
//        curl_setopt($ch, CURLOPT_URL, $url);
//        curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
//        curl_setopt($ch, CURLOPT_HEADER, 0);
//        curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
//        $output = curl_exec($ch);
//
//        curl_close($ch); //关闭连接
//        return $output;
//    }

    public function qmGet($url, $headers, $params = [])
    {
        if (!empty($params)) {
            $url .= '?' . http_build_query($params);
        }

        $ch = curl_init();
        curl_setopt($ch, CURLOPT_URL, $url);
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
        curl_setopt($ch, CURLOPT_HEADER, 0);
        curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
        $output = curl_exec($ch);

//        if ($output === false) {
//            // 可选：处理 cURL 错误
//            $error = curl_error($ch);
//            curl_close($ch);
//            throw new Exception('cURL Error: ' . $error);
//        }

        curl_close($ch); // 关闭连接
        return $output;
    }
}
